package org.lex.notepad.ui;

import java.awt.Window;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.Action;
import javax.swing.JOptionPane;

import org.lex.swing.ActionBuilder;
import org.lex.swing.Option;
import org.lex.swing.OptionType;
import org.lex.swing.Swings;
import org.lex.swing.widgets.OptionDialog;


public class UnsavedChangedDialog {
	private UnsavedChangedDialog() {
	}

	public static Option showDialog(Window parent, File file) {
		OptionDialog dialog = new OptionDialog();
		dialog.setTitle("Unsaved Changes");
		dialog.setMessageType(JOptionPane.QUESTION_MESSAGE);
		dialog.setMessage(getMessage(file));
		dialog.setOptionType(OptionType.YES_NO_CANCEL_OPTION);
		dialog.setActions(getActions());
		return dialog.show(parent);
	}

	private static String getMessage(File file) {
		StringBuilder buffer = new StringBuilder("<html>");
		buffer.append("The content in the '");
		if (null == file)
			buffer.append(NotepadActions.DEFAULT_FILE_NAME);
		else
			buffer.append(file.getName());
		buffer.append("' has changed.<br>");
		buffer.append("What would you like to do?</html>");
		return buffer.toString();
	}

	private static Action[] getActions() {
		Action save = new ActionBuilder("Save").setTooltip("Save the changes, and then close file.").setMnemonic(KeyEvent.VK_S)
				.setIcon(Swings.directoryIcon());
		Action notSave = new ActionBuilder("Don't Save").setTooltip("Discard the unsaved changes.").setMnemonic(KeyEvent.VK_N);
		Action cancel = new ActionBuilder("Cancel").setTooltip("Go back to Notepad.");
		return new Action[] { save, notSave, cancel };
	}
}
